/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminator.utils;

import exterminator.Main;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioSource;
import com.jme3.math.FastMath;
import java.util.HashMap;
import java.util.Map;
import tonegod.gui.core.Screen;

/**
 *
 * @author t0neg0d
 */
public class AudioManager extends AbstractAppState {
	public static enum FXKey {
		SPLAT_1,
		SPLAT_2,
		SPLAT_3,
		WATER,
		CHIME,
		BUG_1,
		BUG_2,
		BUG_3,
		BUG_4,
		BUG_5,
		BUG_6,
		WIN,
		LOSE
	}
	public static enum MusicKey {
		INTRO,
		LOOP
	}
	private Map<FXKey,AudioNode> fx = new HashMap();
	private Map<MusicKey,AudioNode> music = new HashMap();
	private Map<MusicKey, AudioNode> playing = new HashMap();
	
	private Main main;
	private Screen screen;
	private AudioNode musicNode;
	private MusicKey musicKey;
	private AudioNode waterNode;
	
	private float musicVolume = 1;
	private float fxVolume = 1;
	private float musicSpeed = 1;
	
	public AudioManager(Main main, Screen screen) {
		this.main			= main;
		this.screen			= screen;
		
		initAudio();
	}
	
	private void initAudio() {
		AudioNode node;
		node = new AudioNode(main.getAssetManager(),"Sounds/music.wav",false,false);
		node.setReverbEnabled(false);
		music.put(MusicKey.INTRO, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/music2.wav",false,false);
		node.setReverbEnabled(false);
		music.put(MusicKey.LOOP, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/splat01.wav",false,false);
		node.setReverbEnabled(true);
		fx.put(FXKey.SPLAT_1, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/splat02.wav",false,false);
		node.setReverbEnabled(true);
		fx.put(FXKey.SPLAT_2, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/splat03.wav",false,false);
		node.setReverbEnabled(true);
		fx.put(FXKey.SPLAT_3, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/water.wav",false,false);
		node.setReverbEnabled(true);
		fx.put(FXKey.WATER, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/chime.wav",false,false);
		node.setReverbEnabled(true);
		fx.put(FXKey.CHIME, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/bug01.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.BUG_1, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/bug02.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.BUG_2, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/bug03.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.BUG_3, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/bug04.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.BUG_4, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/bug05.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.BUG_5, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/bug06.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.BUG_6, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/win.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.WIN, node);
		node = new AudioNode(main.getAssetManager(),"Sounds/lose.wav",false,false);
		node.setReverbEnabled(false);
		fx.put(FXKey.LOSE, node);
	}
	
	public void playAudio(FXKey key) {
		playAudio(key, 1f*fxVolume);
	}
	
	public void playAudio(FXKey key, float volume) {
		AudioNode node;
		node = fx.get(key);
		node.setVolume(volume*fxVolume);
		float pitch = 1.0f;
		float alt = FastMath.nextRandomFloat()*0.05f;
		if (FastMath.rand.nextBoolean()) alt = -alt;
		pitch += alt;
		node.setPitch(pitch);
		node.playInstance();
	}
	
	public void startMusic() {
		musicNode = music.get(MusicKey.INTRO);
		musicNode.setReverbEnabled(false);
		musicKey = MusicKey.INTRO;
		musicNode.setVolume(musicVolume);
		musicNode.play();
	}
	
	public void playLoopedAudio(FXKey key) {
		if (key == FXKey.WATER) {
			waterNode = fx.get(key);
			waterNode.setVolume(fxVolume);
			waterNode.setLooping(true);
			waterNode.play();
		}
	}
	
	public void stopLoopedAudio(FXKey key) {
		if (key == FXKey.WATER && waterNode != null)
			waterNode.stop();
	}
	
	public void updateMusicVolume(float musicVolume) {
		this.musicVolume = musicVolume;
		if (musicNode.getStatus() == AudioSource.Status.Playing) {
			musicNode.setVolume(musicVolume);
		}
	}
	
	public void updateFXVolume(float fxVolume) {
		this.fxVolume = fxVolume;
	}
	
	public void updateMusicSpeed(float speed) {
		this.musicSpeed = speed;
		this.musicNode.setPitch(speed);
	}
	
	@Override
	public void initialize(AppStateManager stateManager, Application app) {
        
    }
	
	@Override
	public void update(float tpf) {
		if (musicNode != null) {
			if (musicKey == MusicKey.INTRO) {
				if (musicNode.getStatus() == AudioSource.Status.Stopped) {
					musicNode = music.get(MusicKey.LOOP);
					musicNode.setReverbEnabled(false);
					musicNode.setVolume(musicVolume);
					musicNode.setPitch(musicSpeed);
					musicNode.setLooping(true);
					musicNode.play();
				}
			}
		}
	}
	
	@Override
	public void cleanup() {
		
	}
}
